1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include<iostream> #include<cstdio> #include<cstring> #include<cctype> #include<algorithm> #include<cmath> using namespace std; inline int read(){ int w=0,x=0;char c=getchar(); while(!isdigit(c))w|=c=='-',c=getchar(); while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar(); return w?-x:x; } namespace star { const int maxn=1e5+10; int n,m,siz[maxn],fa[maxn],a[maxn]; double ans; struct edge{ int u,v,d; inline bool operator < (const edge &zp) const{return d>zp.d;} }e[maxn]; inline void onion(int x,int y,int k){ if(siz[x]<siz[y])swap(x,y); ans+=1.0*siz[x]*siz[y]*k; fa[y]=x,siz[x]+=siz[y]; } int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);} inline void work(){ n=read(),m=read(); for(int i=1;i<=n;i++) a[i]=read(),fa[i]=i,siz[i]=1; for(int u,v,i=1;i<=m;i++) u=read(),v=read(),e[i]=(edge){u,v,min(a[u],a[v])}; sort(e+1,e+1+m); for(int cnt=0,i=1;i<=m and cnt<=n-1;i++){ int u=find(e[i].u),v=find(e[i].v); if(u!=v) onion(u,v,e[i].d),cnt++; } printf("%.5lf\n",ans/n/(n-1)*2); } } signed main(){ star::work(); return 0; }
|